package progressBugBusiness;

import com.alibaba.fastjson.JSONArray;
import easyDAO.SqlDispatch;
import impl.TestServerBusinessImpl;
import models.MyConfig;
import models.ReturnJson;
import models.SqlReturnModel;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Objects;

/**
 * Created by yangxianming on 2021/2/19.
 * x
 */
public class GetVersion implements TestServerBusinessImpl {

    @Override
    public String doIt(HttpServletRequest request, MyConfig myConfig){
        ReturnJson returnJson = new ReturnJson();
        SqlDispatch sqlDispatch = new SqlDispatch(myConfig);
        JSONArray returnVersion = new JSONArray();
        String action = request.getParameter("action");

        //根据拿到迭代版本
        //progresscase从progress表总获取版本列表
        if (Objects.equals("getVersion", action)){
            String sql = "SELECT progressname FROM progress ORDER BY createtime DESC";
            SqlReturnModel sqlReturnModel = sqlDispatch.selectWord(sql);
            returnVersion.add("");
            for (ArrayList<String> line: sqlReturnModel.getSelectReturnData()){
                returnVersion.add(line.get(0));
            }
            returnJson.setResult(returnVersion);
            returnJson.setApiStatus((sqlReturnModel.getIsSuccess()) ? 1 : 0);
            returnJson.setErrorMsg(sqlReturnModel.getFailingReason());
        }

        return returnJson.toString();
    }

}
